package com.sctvcloud.renhe.ui.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.iflytek.cloud.thirdparty.V;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.beans.EarnIntegralRule;
import com.sctvcloud.renhe.beans.LiveItem;
import com.sctvcloud.renhe.beans.NewsItem;
import com.sctvcloud.renhe.beans.SearchDataBean;
import com.sctvcloud.renhe.beans.TopicItem;
import com.sctvcloud.renhe.ui.util.IListShowData;
import com.sctvcloud.renhe.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

public class SearchNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //0 输入搜索关键词， 1 错误提示， 2 搜索结果， 3 更多
    public static final int INPUT_KEY_TYPE = 0;
    public static final int NOTES_TYPE = 1;
    public static final int SEARCH_RESULT_TYPE = 2;
    public static final int LOAD_MORE_TYPE = 3;
    public static final int SEARCH_ING = 4;


    private List<SearchDataBean> dataList = new ArrayList<>();
    private Context ctx;
    private OnItemBtnClick listener;

    public SearchNewsAdapter(Context ctx, List<SearchDataBean> dataList, OnItemBtnClick listener) {
        this.ctx = ctx;
        this.dataList = dataList;
        this.listener = listener;
    }

    static class SearchViewHolder extends RecyclerView.ViewHolder {
        LinearLayout inputKeyLayout;
        LinearLayout notesLayout;
        LinearLayout newsLayout;
        LinearLayout loadMoreLayout;
        CustomFontTextView searchKey;
        CustomFontTextView notes;
        ImageView searchingImg;
        CustomFontTextView moreNews;
        CustomFontTextView newsTitle;
        CustomFontTextView newsTime;
        CustomEXImageView new_img;
        ImageView video_play;

        public SearchViewHolder(View view) {
            super(view);
            inputKeyLayout = (LinearLayout) view.findViewById(R.id.input_layout);
            notesLayout = (LinearLayout) view.findViewById(R.id.note_layout);
            newsLayout = (LinearLayout) view.findViewById(R.id.news_layout);
            loadMoreLayout = (LinearLayout) view.findViewById(R.id.load_more_layout);
            searchKey = (CustomFontTextView) view.findViewById(R.id.search_key);
            notes = (CustomFontTextView) view.findViewById(R.id.notes);
            searchingImg = (ImageView)view.findViewById(R.id.searching_img);
            newsTitle = (CustomFontTextView) view.findViewById(R.id.new_title);
            newsTime = (CustomFontTextView) view.findViewById(R.id.new_time);
            moreNews = (CustomFontTextView) view.findViewById(R.id.more_news);
            new_img = (CustomEXImageView) view.findViewById(R.id.new_img);
            video_play = (ImageView) view.findViewById(R.id.video_play);

        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.search_news_item, parent, false);
        holder = new SearchViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != dataList && dataList.size() > position) {
            final SearchDataBean data = dataList.get(position);
            SearchViewHolder h = (SearchViewHolder) holder;
            h.inputKeyLayout.setVisibility(View.GONE);
            h.notesLayout.setVisibility(View.GONE);
            h.newsLayout.setVisibility(View.GONE);
            h.loadMoreLayout.setVisibility(View.GONE);
            h.video_play.setVisibility(View.GONE);
            if (data.getType() == INPUT_KEY_TYPE) {
                h.inputKeyLayout.setVisibility(View.VISIBLE);
                h.searchKey.setText(data.getNotes());
            } else if (data.getType() == NOTES_TYPE) {
                h.notesLayout.setVisibility(View.VISIBLE);
                h.notes.setVisibility(View.VISIBLE);
                h.searchingImg.setVisibility(View.GONE);
                h.notes.setText(data.getNotes());
            } else if (data.getType() == SEARCH_RESULT_TYPE) {
                h.newsLayout.setVisibility(View.VISIBLE);
                IListShowData itemData = data.getItemData();
                if (null != itemData) {
                    if (itemData instanceof NewsItem) {
                        NewsItem item = (NewsItem) itemData;
                        h.newsTitle.setText(item.getNewsTitle());
                        h.newsTime.setText(item.getTime());
                        GlideUtil.getGlideWithMid169Def(ctx, item.getNewsImage()).into(h.new_img);
                        if (item.getNewsType() == 3) {
                            h.video_play.setVisibility(View.VISIBLE);
                            h.video_play.setImageResource(R.mipmap.icon_home_play);
                        } else if (item.getNewsType() == 2) {
                            h.video_play.setVisibility(View.VISIBLE);
                            h.video_play.setImageResource(R.mipmap.icon_home_voice);
                        }
                    } else if (itemData instanceof LiveItem) {
                        LiveItem item = (LiveItem) itemData;
                        h.newsTitle.setText(item.getTitle());
                        h.newsTime.setText(item.getTime());
                        h.video_play.setVisibility(View.VISIBLE);
                        h.video_play.setImageResource(R.mipmap.icon_home_play);
                        GlideUtil.getGlideWithMid169Def(ctx, item.getLiveImg()).into(h.new_img);
                    } else if (itemData instanceof TopicItem) {
                        TopicItem item = (TopicItem) itemData;
                        h.newsTitle.setText(item.getNewsTitle());
                        h.newsTime.setText(item.getTime());
                        GlideUtil.getGlideWithMid169Def(ctx, item.getNewsImage()).into(h.new_img);
                    }
                }
                h.newsLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != listener) {
                            listener.onItemBtnClick(data.getItemData());
                        }
                    }
                });
            } else if (data.getType() == LOAD_MORE_TYPE) {
                h.loadMoreLayout.setVisibility(View.VISIBLE);
                h.moreNews.setText("查看更多");
                h.loadMoreLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != listener) {
                            listener.onLoadMore(data.getNotes());
                        }
                    }
                });
            }else if (data.getType() == SEARCH_ING) {
                //正在搜索。。。
                h.notesLayout.setVisibility(View.VISIBLE);
                h.notes.setVisibility(View.GONE);
                h.searchingImg.setVisibility(View.VISIBLE);
                AnimationDrawable anim = (AnimationDrawable)  h.searchingImg.getDrawable();
                anim.start();
            }
        }
    }

    public int setDataList(List<SearchDataBean> list) {
        int pos = getItemCount();
        this.dataList = list;
        if (pos > 0) {
            notifyItemChanged(pos - 1);
        }
//        notifyDataSetChanged();
        return getItemCount() - 1;
    }

    public void setMoreNewsList(List<SearchDataBean> list) {
        this.dataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (null != dataList) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    public SpannableStringBuilder setNumColor(String str) {
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        for (int i = 0; i < str.length(); i++) {
            char a = str.charAt(i);
            if (a >= '0' && a <= '9') {
                style.setSpan(new ForegroundColorSpan(ctx.getResources().getColor(R.color.colorLightRed)), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return style;
    }

    public interface OnItemBtnClick {
        void onItemBtnClick(IListShowData data);

        void onLoadMore(String keyword);
    }
}

